package de.jmda.mview.proj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.apache.log4j.Logger;

import de.jmda.mview.DesktopPane.JAXBModel;

public class DiagramsTreeDiagramPopup implements ActionListener
{
	private final static Logger LOGGER =
      Logger.getLogger(DiagramsTreeDiagramPopup.class);

	/**
	 * Enumerates the menu items for the popup.
	 */
	private enum Items
	{
		RENAME("rename"),
		OPEN(  "open"),
		CLOSE( "close"),
		DELETE("delete"),
		;

		private String name;

		private Items(String name)
		{
			this.name = name;
		}
	}

	/**
	 * event that represents the tree node click
	 */
	private DiagramsTreeNodeClicked diagramsTreeNodeClicked;

	/**
	 * diagram model of the clicked tree node
	 */
	private JAXBModel diagramModel;

	/**
	 * shortcut to {@link DiagramsTreeNodeClicked#getDiagramsController()}
	 */
	private DiagramsController diagramsController;
	
	/**
	 * shortcut to {@link DiagramsTreeNodeClicked#getMouseEvent()}
	 */
	private MouseEvent mouseEvent;

	private JPopupMenu menu;

	/**
	 * @param diagramsTreeNodeClicked
	 * @param diagramModel
	 */
	public DiagramsTreeDiagramPopup(
			DiagramsTreeNodeClicked diagramsTreeNodeClicked,
			JAXBModel diagramModel)
	{
		super();

		this.diagramsTreeNodeClicked = diagramsTreeNodeClicked;
		this.diagramModel = diagramModel;
		
		diagramsController = diagramsTreeNodeClicked.getDiagramsController();
		mouseEvent = diagramsTreeNodeClicked.getMouseEvent();
		
		menu = initialiseMenu();
	}

	private JPopupMenu initialiseMenu()
  {
		menu = new JPopupMenu();

		// no restriction for renaming of nodes
		JMenuItem itemRename = new JMenuItem(Items.RENAME.name);
		itemRename.addActionListener(this);
		menu.add(itemRename);
		
		if (diagramsController.displayOptionOpenDiagram(diagramModel))
		{
			JMenuItem itemOpenDiagram = new JMenuItem(Items.OPEN.name);
			itemOpenDiagram.addActionListener(this);
			menu.add(itemOpenDiagram);
		}
		else
		{
			JMenuItem itemCloseDiagram = new JMenuItem(Items.CLOSE.name);
			itemCloseDiagram.addActionListener(this);
			menu.add(itemCloseDiagram);
		}

		// no restriction for removal of nodes
		JMenuItem itemDelete = new JMenuItem(Items.DELETE.name);
		itemDelete.addActionListener(this);
		menu.add(itemDelete);
		
		return menu;
  }

	public void run()
	{
		menu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent)
	{
		if (actionEvent.getActionCommand().equals(Items.RENAME.name))
		{
			rename();
		}
		else if (actionEvent.getActionCommand().equals(Items.OPEN.name))
		{
			open();
		}
		else if (actionEvent.getActionCommand().equals(Items.CLOSE.name))
		{
			close();
		}
		else if (actionEvent.getActionCommand().equals(Items.DELETE.name))
		{
			delete();
		}
		else
		{
			LOGGER.warn(
					"unexpected action event with action command " +
					actionEvent.getActionCommand());
		}
	}

	private void rename()
  {
		diagramsController.getDiagramsControllerContext()
		                  .getTreeDiagrams()
		                  .startEditingAtPath(
		                  		diagramsTreeNodeClicked.getTreePath());
  }

	private void open()
  {
		diagramsController.openDiagram(diagramModel);
  }

	private void close()
  {
		diagramsController.closeDiagram(diagramModel);
  }

	private void delete()
  {
		diagramsController.deleteDiagram(diagramModel);
  }
}